package com.web;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.User;
import com.service.UserService;
import com.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    /**
     * 根据username获取user对象
     * API接口说明
     * GET方法 接受username参数
     * username="张三"
     * 存在相符合的user对象返回"successful"
     * 不存在相符合的user对象返回"unsuccessful"
     */
    public void selectByUsername(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");

        User user = userService.selectByUsername(username);

        if (user != null) {
            //存在对应的user对象，回应成功标志
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write("successful");
        } else {
            //不存在对应的user对象，回应不成功标志
            response.getWriter().write("unsuccessful");
        }
    }


    /**
     * 根据username和password登录
     * API接口说明
     * POST方法 接受username和password参数
     * {"username":"张三", "password":"123456”}
     * 存在相符合的user对象返回"login successful"
     * 不存在相符合的user对象返回"login unsuccessful"
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();

        JSONObject jsonObject = JSON.parseObject(params);


        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        try {
            String username = jsonObject.get("username").toString();
            String password = jsonObject.get("password").toString();

            User user = userService.select(username, password);


            if (user != null) {
                //存在对应的user对象，回应成功标志
                response.setContentType("text/json;charset=utf-8");
                response.getWriter().write(username);
//                if (username.equals("manager")) {
//                    response.getWriter().write(username);
//                } else if (username.equals("admin")) {
//                    response.getWriter().write(username);
//                } else {
//                    response.getWriter().write(username);
//                }
            } else {
                //不存在对应的user对象，回应不成功标志
                response.getWriter().write("login unsuccessful");
            }
        } catch (Exception e) {
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write("需要请求参数");
        }


    }

}
